package com.j.lemon.learn.basic.filter;

import org.springframework.core.annotation.Order;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@Order(1)
public class TestFilter2 implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        System.out.println("Testfilter2 doFilter 前");

//        HttpServletResponse response = (HttpServletResponse) servletResponse;
//        response.setContentType("text/html;charset=UTF-8");
//        PrintWriter writer = response.getWriter();
//        writer.write("注册成功，3秒后跳转到主页");
//        writer.flush();
//        writer.close();
//        response.setHeader("refresh", "3;url=/api/limit");


//        HttpServletRequest request = (HttpServletRequest) servletRequest;
//        String requestURI = request.getRequestURI();
//        StringBuffer requestURL = request.getRequestURL();
//        //    /rest/test
//        System.out.println(requestURI);
//        //    http://localhost:8088/rest/test
//
//        System.out.println(requestURL);
//
//        if(requestURI.contains("rest")){
//            request.getRequestDispatcher("/public/404.jsp").forward(servletRequest,servletResponse);
//        }else{
//            filterChain.doFilter(servletRequest,servletResponse);
//        }
        filterChain.doFilter(servletRequest, servletResponse);
        System.out.println("Testfilter2 doFilter 后");

    }

    @Override
    public void destroy() {
        System.out.println("test2 has been destroy");
    }
}
